import { BlockType, ToolType, ItemDrop } from './types';
import { getBlock } from './BlockRegistry';
import { Vec3 } from '@/core/types';

export interface BreakResult {
  success: boolean;
  drops: ItemDrop[];
  breakTime: number;
}

export class BlockBreaker {
  public calculateBreakTime(blockType: BlockType, tool?: ToolType): number {
    const block = getBlock(blockType);
    const baseTime = block.properties.hardness;
    
    // 如果方块需要特定工具但没有使用正确工具，增加破坏时间
    if (block.properties.tool && block.properties.tool !== tool) {
      return baseTime * 5; // 使用错误工具时破坏时间增加5倍
    }
    
    // 使用正确工具时的破坏时间
    const toolMultiplier = this.getToolMultiplier(tool);
    return Math.max(0.1, baseTime / toolMultiplier);
  }

  private getToolMultiplier(tool?: ToolType): number {
    switch (tool) {
      case ToolType.PICKAXE:
        return 4.0; // 镐子对石头类方块效果好
      case ToolType.SHOVEL:
        return 4.0; // 铲子对土壤类方块效果好
      case ToolType.AXE:
        return 4.0; // 斧子对木头类方块效果好
      case ToolType.HOE:
        return 2.0; // 锄头效果一般
      default:
        return 1.0; // 徒手破坏
    }
  }

  public breakBlock(blockType: BlockType, tool?: ToolType): BreakResult {
    const block = getBlock(blockType);
    
    // 某些方块无法破坏
    if (block.properties.hardness >= 100) {
      return {
        success: false,
        drops: [],
        breakTime: Infinity
      };
    }

    const breakTime = this.calculateBreakTime(blockType, tool);
    const drops = this.calculateDrops(blockType, tool);

    return {
      success: true,
      drops,
      breakTime
    };
  }

  private calculateDrops(blockType: BlockType, tool?: ToolType): ItemDrop[] {
    const block = getBlock(blockType);
    const drops: ItemDrop[] = [];

    // 如果需要特定工具但没有使用，可能不掉落物品
    if (block.properties.tool && block.properties.tool !== tool) {
      // 某些方块（如矿物）需要正确工具才掉落
      if (this.requiresCorrectTool(blockType)) {
        return []; // 不掉落任何物品
      }
    }

    // 计算每个可能的掉落物
    for (const drop of block.properties.drops) {
      if (Math.random() <= drop.chance) {
        drops.push({
          type: drop.type,
          count: drop.count,
          chance: 1.0 // 已经通过概率检查，设为100%
        });
      }
    }

    return drops;
  }

  private requiresCorrectTool(blockType: BlockType): boolean {
    // 这些方块需要正确的工具才能掉落物品
    const requiresToolBlocks = [
      BlockType.STONE,
      BlockType.COAL_ORE,
      BlockType.IRON_ORE,
      BlockType.GOLD_ORE,
      BlockType.DIAMOND_ORE,
      BlockType.OBSIDIAN
    ];

    return requiresToolBlocks.includes(blockType);
  }

  public canBreakBlock(blockType: BlockType): boolean {
    const block = getBlock(blockType);
    return block.properties.hardness < 100;
  }

  public getRecommendedTool(blockType: BlockType): ToolType {
    const block = getBlock(blockType);
    return block.properties.tool || ToolType.NONE;
  }

  public isEffectiveTool(blockType: BlockType, tool: ToolType): boolean {
    const recommendedTool = this.getRecommendedTool(blockType);
    return recommendedTool === ToolType.NONE || recommendedTool === tool;
  }
}